﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.ComponentModel;
using VPLink2013.Framework;

namespace VPLink2013.Webparts
{
    public class FusionNews : WebpartVPLink
    {
        [Category("Vente privée")]
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("Url de la liste 1")]
        public string list1Url { get; set; }

        [Category("Vente privée")]
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("ID item liste 1")]
        public string strID1 { get; set; }

        [Category("Vente privée")]
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("Url de la liste 2")]
        public string list2Url { get; set; }

        [Category("Vente privée")]
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("ID item liste 2")]
        public string strID2 { get; set; }

        [Category("Vente privée")]
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("XSl d'affichage")]
        public string strXslFilePath { get; set; }

        private void AddAnnonce(SPListItem item)
        {
            Annonce ann = new Annonce();
            ann.Titre = VPLink2013SPHelper.GetValue(item, "Titre");
            if (string.IsNullOrEmpty(ann.Titre))
            {
                ann.Titre = VPLink2013SPHelper.GetValue(item, "Title");
            }

            ann.Body = VPLink2013SPHelper.GetValue(item, "Contenu");
            if (string.IsNullOrEmpty(ann.Body))
            {
                ann.Body = VPLink2013SPHelper.GetValue(item, "Body");
            }

            ann.Ordre = VPLink2013SPHelper.GetValue(item, "Ordre");
            global.ListeAnnonces.Add(ann);
        }

        private void GetAnnonce(string url, string itemId)
        {
            if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(itemId))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = site.OpenWeb(url))
                        {
                            SPList targetList = web.GetList(url);
                            if (targetList != null)
                            {
                                SPListItem item1 = targetList.GetItemById(Int32.Parse(itemId));
                                AddAnnonce(item1);
                            }
                        }
                    }
                });
            }
        }


        private string getXslTransformation()
        {
            string strTransformation = string.Empty;
            string strXslt = string.Empty;
            if (!string.IsNullOrEmpty(strXslFilePath))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            strXslt = web.GetFileAsString(strXslFilePath);
                            string strXml = FileSerialization.CreateXmlStringFromObj(global);

                            if (!string.IsNullOrEmpty(strXslt) && !string.IsNullOrEmpty(strXml))
                            {
                                strTransformation = GetTransformFromStyleLibrary(strXml, strXslt);
                            }
                        }
                    }
                });
            }
            return strTransformation;
        }

        protected override string GetHtml()
        {
            string strHtml = string.Empty;

            global = new GlobalVPLink2013();
            global.ListeAnnonces = new List<Annonce>();
            ContextTrace.AddTrace(string.Format("Url du site courant : {0}", SPContext.Current.Site.Url));
            GetAnnonce(list1Url, strID1);
            GetAnnonce(list2Url, strID2);
            strHtml = getXslTransformation();
            return strHtml;
        }
    }
}
